package com.china08.yxyapi.api.schwebsite.index;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSetting;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.repository.db.SwsSettingRepository;

@CrossOrigin
@RestController("SwsIndexForAppProtectController")
public class SwsIndexForAppProtectController extends YxyApiBaseController {

    @Autowired
    private SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Autowired
    private SwsSettingRepository swsSettingRepository;

    @RequestMapping(value = "/api_yw/schwebsite/index/forapp/protect", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        String domainName = getDomainName(request);
        Assert.isTrue(StringUtils.isNotBlank(domainName), "域名为空");
        // 通过域名获取学校ID
        SwsSettingDomains byDomainName = swsSettingDomainsRepository.findByDomainName(domainName);
        Assert.isTrue(null != byDomainName, "域名对象为空");
        String schoolId = byDomainName.getSchoolId();
        SwsSetting swsSetting = swsSettingRepository.findBySchoolId(schoolId);
        Assert.isTrue(null != swsSetting, "学校信息为空");
        RespModel respModel = new RespModel();
        if (null != swsSetting) {
            String schoolBadge = swsSetting.getSchoolBadge();
            String schoolName = "隐私保护";
            String description = "保护用户个人信息是我们的一项基本原则，我们将会采取合理的措施保护用户的个人信息。，未经用户许可我们不会向第三方公开、透露用户个人信息。但以下情况除外";
            if (StringUtils.isBlank(schoolBadge)) {
                schoolBadge = "";
            }
            respModel.schoolBadge = schoolBadge;
            respModel.schoolName = schoolName;
            respModel.description = description;
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {

        public String schoolBadge;

        public String schoolName;

        public String description;

    }

}
